/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : ArithmaticAction.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Jan 27, 2011  mycodetalks			       Created
 */
package net.lwf.web.struts.action.maths;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lwf.handler.IProcessHandler;
import lwf.handler.entities.RadioData;
import lwf.web.struts.action.constant.IActionConstant;
import net.lwf.common.constant.ICommonConstant;
import net.lwf.entity.FormBasicInfo;
import net.lwf.entity.QuestionInfo;
import net.lwf.handler.constant.IHandlerConstant;
import net.lwf.handler.math.arithmatics.ArithmaticProcessHandler;
import net.lwf.jaxb.entity.StaticWorkSheet;
import net.lwf.jaxb.service.XMLDataService;
import net.lwf.jaxb.service.maths.worksheet.StaticWorkSheetService;
import net.lwf.util.web.WebUtil;
import net.lwf.web.constant.IPageTitle;
import net.lwf.web.constant.IWebConstant;
import net.lwf.web.struts.action.LwfWorksheetActionForm;
import net.lwf.web.struts.action.worksheet.StaticWorkSheetAction;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * Class description goes here
 */
public class ArithmaticAction extends StaticWorkSheetAction {
	
	//private static final String prefix = "Arithmatics";
	private String operation; //Remove 05/09/2011
	
	public ActionForward home(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		WebUtil.removeAttribute(request, IWebConstant.DECIMALS);
		this.setRequestData(request);
		this.operation = getOperation(request);
		ArithmaticActionForm aaform = (ArithmaticActionForm) form;
		aaform.setAskQuestion(false);
		aaform.setOperation(operation.toLowerCase());

		return mapping.findForward("home");
	} //home
	
	public void setRequestData(HttpServletRequest request) {
		String decimals = request.getParameter(IWebConstant.DECIMALS);
		if(decimals != null) {
			WebUtil.setAttribute(request, IWebConstant.DECIMALS, decimals);
		}
	}

	public ActionForward generate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		//String operation = getOperation(request);
		this.operation = getOperation();
		//this.operation = (String) WebUtil.getAttribute(request, IWebConstant.OPERATION);
		//Get Handler
		ArithmaticProcessHandler handler = (ArithmaticProcessHandler) getHandler(request);
		QuestionInfo aq = handler.generateQuestion();
		//Set the question data
		setQuestionData(request, aq);
		//Set form data
		ArithmaticActionForm aaform = (ArithmaticActionForm) form;
		aaform.setOperation(operation.toLowerCase());
		aaform.setHtmlQuestion(aq.getHtmlQuestion());
		aaform.setAskQuestion(true);
		//setBasicFormInfo(request, getTitle(operation, prefix), getQuizLink(), getPdfLink());
		aaform.setShowRemainderTextBox(handler.showRemainderTextBox());		
		aaform.setQuestionNumber(getQuestionNumber(request));
		aaform.setInstructionText(this.getInstructionText());
		return mapping.findForward("success");
	} //generate
	
	//------------------------------------------------------------------------------------------------------------
	// For display of static worksheets
	//------------------------------------------------------------------------------------------------------------
	public ActionForward chooseoperation(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		//Remove basic form attribute
		request.removeAttribute(IWebConstant.BASIC_FORM_INFO); 	// to make sure the dynamic pdf and quiz links not to display
		LwfWorksheetActionForm swaform = (LwfWorksheetActionForm) form;
		swaform.setRadioData(getOperationsData());
		return mapping.findForward("chooseoperation");
	} //chooseoperation
	
	public ActionForward wshome(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		String subject = "maths";
		String topic = request.getParameter("operation");
		
		//First remove the dynamic pdf link from the 
		FormBasicInfo basicforminfo = (FormBasicInfo) request.getAttribute(IWebConstant.BASIC_FORM_INFO);
		if(basicforminfo != null) {
			basicforminfo.setPdfLink(ICommonConstant.EMPTY);
			basicforminfo.setPageTitle(getOperation() + ICommonConstant.EMPTY_SPACE + "worksheets");
			//request.setAttribute(IWebConstant.BASIC_FORM_INFO, basicforminfo);
		}
			
		LwfWorksheetActionForm swaform = (LwfWorksheetActionForm) form;
		swaform.setSubject(subject);
		swaform.setTopic(topic);
		try {
			//For static
			StaticWorkSheetService service = new StaticWorkSheetService();
			List<StaticWorkSheet> worksheets = service.getUniqueWorkSheets(getGrade(), subject, topic);
			this.setPrintKey(worksheets);
			swaform.setWorksheets(worksheets);			
		}
		catch(Exception ex) {
			//Do nothing
		}
		//For Dynamic worksheets
		String pdfLink = IActionConstant.ARITHMATICS_PDF_LINK + "&operation=" + getOperation();
		swaform.setDynamicPdfLink(pdfLink);		
		swaform.setShowBackLink(true);
		//swaform.setRadioData(getOperationsData());
		//swaform.setDynamicPdfLinkName(this.getDynamicPdfLinkName(topic));
		swaform.setDynamicPdfLinkName("Additional worksheets");
		return mapping.findForward("worksheets");
	} //home
	
	private List<RadioData> getOperationsData() {
		List<RadioData> radiodata = new ArrayList<RadioData>();
		XMLDataService service = new XMLDataService();
		List<String> operations = service.getOperations(Integer.parseInt(getGrade()));
		for(String data: operations) {
			RadioData radio = new RadioData(data, data);
			radiodata.add(radio);
		}
		return radiodata;
	}
	
/*	private String getDynamicPdfLinkName(String operation) {
		if(operation.equalsIgnoreCase(IHandlerConstant.ADDITION)) {
			return "dynamic addition worksheets";
		}
		else if(operation.equalsIgnoreCase(IHandlerConstant.SUBTRACTION)) {
			return "dynamic subtraction worksheets";
		}
		else if(operation.equalsIgnoreCase(IHandlerConstant.MULTIPLICATION)) {
			return "dynamic multiplication worksheets";
		}
		else  if(operation.equalsIgnoreCase(IHandlerConstant.DIVISION)) {
			return "dynamic division worksheets";
		}
		return null;
	}*/
	//---------------------------------- End of static worksheets

	
	@Override
	public IProcessHandler getHandler(HttpServletRequest request) {
		//return new ArithmaticProcessHandler(getGrade(), getOperation(request));
		String decimals = (String) WebUtil.getAttribute(request, IWebConstant.DECIMALS);
		if(decimals != null) {
			return new ArithmaticProcessHandler(getGrade(), getOperation(), true);
		}
		return new ArithmaticProcessHandler(getGrade(), getOperation());
	}
		
/*	public void resetOperation(HttpServletRequest request) {
		//this.operation = request.getParameter(IWebConstant.TOPIC);
		this.operation = request.getParameter(IWebConstant.OPERATION);
		WebUtil.setAttribute(request, IWebConstant.OPERATION, this.operation);
	}

	public String getOperation() {
		return operation;
	}*/
	
	public String getTitle(String operation, String prefix) {
		String title = prefix + " - ";
		if(operation.equalsIgnoreCase(IHandlerConstant.ADDITION)) {
			title += "Addition";
		}
		else if(operation.equalsIgnoreCase(IHandlerConstant.SUBTRACTION)) {
			title += "Subtraction";
		}
		else if(operation.equalsIgnoreCase(IHandlerConstant.MULTIPLICATION)) {
			title += "Multiplication";
		}
		else  if(operation.equalsIgnoreCase(IHandlerConstant.DIVISION)) {
			title += "Division";
		}
		return title;
	}

	@Override
	public String getPdfLink() {
		return IActionConstant.ARITHMATICS_PDF_LINK;
	}

	@Override
	public String getQuizLink() {
		return IActionConstant.ARITHMATICS_QUIZ_LINK;
	}
	
	@Override
	public String getPageTitle() {
		if(getOperation().equalsIgnoreCase(IHandlerConstant.ADDITION)) {
			return IPageTitle.ADDITION;
		}
		else if(getOperation().equalsIgnoreCase(IHandlerConstant.SUBTRACTION)) {
			return IPageTitle.SUBTRACTION;
		}
		else if(getOperation().equalsIgnoreCase(IHandlerConstant.MULTIPLICATION)) {
			return IPageTitle.MULTIPLICATION;
		}
		else  if(getOperation().equalsIgnoreCase(IHandlerConstant.DIVISION)) {
			return IPageTitle.DIVISION;
		}
		return null;
	}

	@Override
	public String getMainTitle() {
		return IPageTitle.ARITHMATICS_MAIN;
	}
	private String getInstructionText() {
		String text = "Solve the following ";
		if(getOperation().equalsIgnoreCase(IHandlerConstant.ADDITION)) {
			text += IHandlerConstant.ADDITION;
		}
		else if(getOperation().equalsIgnoreCase(IHandlerConstant.SUBTRACTION)) {
			text += IHandlerConstant.SUBTRACTION;
		}
		else if(getOperation().equalsIgnoreCase(IHandlerConstant.MULTIPLICATION)) {
			text += IHandlerConstant.MULTIPLICATION;
		}
		else  if(getOperation().equalsIgnoreCase(IHandlerConstant.DIVISION)) {
			text += IHandlerConstant.DIVISION;
		}
		
		return text + " sum.";
	}
}



